package com.xubeihong.xubeihong;

import com.alibaba.fastjson2.JSONObject;
import com.xubeihong.xubeihong.constant.Constant;
import lombok.extern.slf4j.Slf4j;
import org.apache.http.client.config.RequestConfig;
import org.apache.http.client.methods.CloseableHttpResponse;
import org.apache.http.client.methods.HttpPost;
import org.apache.http.entity.ContentType;
import org.apache.http.entity.StringEntity;
import org.apache.http.impl.client.CloseableHttpClient;
import org.apache.http.impl.client.HttpClients;
import org.apache.http.protocol.HTTP;
import org.apache.http.util.EntityUtils;

@Slf4j
public class HttpTool2 {
    // 改id  改签名
    private  static  String url = "https://muspace.smartechnology.com.cn/api/new_active_order";
    //14591
    private static String entityStr = "{\"p\":\"w\",\"Sign\":\"RootSign\",\"active_time_id\":\""+Constant.id+"\",\"contact_phone\":\"1835230\",\"alternate\":\"\",\"toi_cardtype_id\":1,\"spot\":0,\"person\":[{\"toi_cardtype_id\":1,\"toi_username\":\"汪洋\",\"toi_card_num\":\"3326242010\"}],\"notes\":\"\",\"children\":[],\"toi_unit\":\"\",\"uid\":\"P%3AnaeEEQpHhISkkeWnDNwHHw%3D%3D\",\"_sign\":[\"tSuM4eUDpm3T9Atk8xZkinQcxosf/9JCXbUIkyLbqyJnVs1VDIJkRiYzAXIWPVjQMlI60YL0/pZ0aLVQ0aWWJkD49MFz3beZnFkfnL/mwQD1+G9MwFa3ewHML5HnE4v9oQcTXVfuncGkhwQCNBmxNAhhE2ivpekueaAqQV1QSwM+n+cWmyPqflHuwbyjgvSBJ2aSbBvS5uUZrT7czcOBWvfe9BDtVNbsKYQQ/50aosAY5Dl9r7UsnvafOC2bT2VkZ3pHxMU7fH3CR4in4ZHYMHqD6zg3crZe4850D6z4+94jpMDQMtE/RIMpicfFdc9lCpOM5LzSs+RaKT2n544PnQ==\"],\"api_token\":\"0d7e2a3243970f87b71bd5f8a95e60a4\"}";

    public static JSONObject post()  {

        CloseableHttpResponse response ;
        JSONObject result=null;
        CloseableHttpClient httpclient = HttpClients.createDefault();
        HttpPost post = new HttpPost(url);
        RequestConfig requestConfig = RequestConfig.custom()
                .setConnectTimeout(50000) // 连接超时时间，单位毫秒
                .setSocketTimeout(50000)  // 读取超时时间，单位毫秒
                .build();
        post.setConfig(requestConfig);
        post.setHeader("Sec-Fetch-Dest","empty");
        post.setHeader("Sec-Fetch-Site","cross-site");
        post.setHeader("Host","muspace.smartechnology.com.cn");
        post.setHeader("Accept","application/json");
        post.setHeader("User-Agent","Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/122.0.0.0 Safari/537.36 MicroMessenger/7.0.20.1781(0x6700143B) NetType/WIFI MiniProgramEnv/Windows WindowsWechat/WMPF WindowsWechat(0x63090c11)XWEB/11275");
        //post.setHeader("Content-Length","712");
        post.setHeader("xweb_xhr","1");
        post.setHeader("Accept-Language","zh-CN,zh;q=0.9");
        post.setHeader("Accept-Encoding","gzip, deflate, br");
        post.setHeader("Sec-Fetch-Mode","cors");
        post.setHeader("Content-Type","application/json");
        post.setHeader("Referer","https://servicewechat.com/wx607b92668342d8ca/144/page-frame.html");
        post.setHeader("Connection","keep-alive");

        post.setEntity(new StringEntity(entityStr, ContentType.APPLICATION_JSON));


        try{
            response = httpclient.execute(post);
            String body = EntityUtils.toString(response.getEntity(), HTTP.UTF_8);
            result = JSONObject.parseObject(body);
            //System.out.println(result);
            log.info("抢票结果"+result.toString());

        }catch (Exception e){
            log.error("请求接口出错"+e.toString());
            e.printStackTrace();
            JSONObject res = new JSONObject();
            res.put("status",-1);
            return res;
        }


        return result;
    }
}
